package com.heima.user.filter;


import com.heima.common.dto.User;
import com.heima.common.util.AppUserThreadLocal;
import com.heima.common.util.WeMediaThreadLocal;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Service
public class UserFilter extends GenericFilterBean {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;

        String userId = request.getHeader("userId");

        if (StringUtils.isNotEmpty(userId) && !userId.equals("0")) {
            User user = new User();
            user.setUserId(Integer.parseInt(userId));
            AppUserThreadLocal.set(user);
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }
}
